package marcis.jarina.directory.pom;

import marcis.jarina.directory.FileActionListener;
import marcis.jarina.directory.FileHelper;
import marcis.jarina.directory.repository.*;
import marcis.jarina.hibernate.ContinuousSaver;
import marcis.jarina.hibernate.entities.Archive;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class PomProcessor implements FileActionListener {

    private static final Logger log = Logger.getLogger(PomProcessor.class);

    @Autowired
    private ContinuousSaver<Archive> archiveContinuousSaver;


    @Override
    public void onFileFound(File file) {
        final XMLUtil pomUtil;
        Archive archive;
        try {
            pomUtil = new XMLUtil(file);
            archive = buildArchive(pomUtil);
            if(archive.getDependentArchives() != null) {
                log.info("Count of dependent archives:" + archive.getDependentArchives().size());
            }
        } catch (XMLFileException e) {
            log.error("Skipping broken pom file: " + file.getName());
            return;
        }

        File jarFile = FileHelper.getJarFromPom(file);
        if (jarFile != null) {
            archive.setFilePath(jarFile.getPath());
        } else {
            log.warn("Jar file does not exists for pom file: " + file.getPath());
        }
        archiveContinuousSaver.addEntity(archive);
    }


    @Override
    public File getRepositoryDir() {
        return FasterJarProcessor.findRepository();
    }

    @Override
    public void finish() {
        archiveContinuousSaver.save();
    }

    private Archive buildArchive(XMLUtil pomUtil) throws XMLFileException {
        final Archive archive = new Archive();
        archive.setGroupId(pomUtil.getGroupId());
        archive.setArtifactId(pomUtil.getArtifactId());
        String ver = pomUtil.getVersion();
        JarVersion jarVersion2 = new JarVersion(ver);
        archive.setVersion(jarVersion2.getNumericPart());
        SimpleDateFormat sdf = new SimpleDateFormat();
        archive.setStoreDate(sdf.format(new Date()));
        try {
            buildArchiveDependencies(pomUtil, archive);
        } catch (XMLFileException e) {
            log.info("Archive does not depends on other archives.");
        }

        return archive;
    }

    private void buildArchiveDependencies(XMLUtil pomUtil, Archive archive) throws XMLFileException {
        for (Dependency dependency : pomUtil.getALLPomDependencies()) {
            Archive dependentArchive = Archive.convert(dependency);
            if (dependentArchive != null) {
                dependentArchive.setStoreDate(archive.getStoreDate());
                log.debug("adding new dep: " + dependency.toString());
                archive.addDependentArchive(dependentArchive);
            }
        }
    }


}